import { createRouter, createWebHashHistory } from "vue-router";
import userStore from "../store/UserStore"
//路由映射数组
const r:any[] = [
    {
        path:"/", //默认的第一个路由
        redirect:"/start",
    },
    {
        path:"/start",
        component:() => import('../view/start.vue'),
        children:[
            {
                path: "",
                redirect: "/start/login"
            },
            {
                path:"login",
                component:() => import('../view/login.vue') 
            },
            {
                path:"/login_forget",
                component:() => import('../view/login_forget.vue')
            },
        ]
    },
    {
        path:"/home",
        component:() => import('../view/home.vue'),
        children:[
            {
                path: "",
                redirect: "/home/homepage"
            },
            {
                path:"homepage",
                component:() => import('../view/HomePage.vue') 
            },
            {
                path:"personInfo",
                component:() => import('../view/PersonInfo.vue') 
            },
            {
                path:"updatePassword",
                component:() => import('../view/UpdatePassword.vue') 
            },
            {
                path:"punchCard",
                component:() => import('../view/PunchCard.vue') 
            },
            {
                path:"supplement",
                component:() => import('../view/Supplement.vue') 
            },
            {
                path:"deptlist",
                component:() => import('../view/deptlist.vue') 
            },
            {
                path:"addDept",
                component:()=> import('../view/addDept.vue')
            },
            {
                path:"modifydept",
                component:()=> import('../view/modifydept.vue')
            },
            {
                path:"calendar",
                component:()=> import('../view/Calendar.vue')
            },
            {
                path:"app4vocation",
                component:()=> import('../view/app4vocation.vue')
            },
            {
                path:"approveAttend",
                component:()=> import('../view/approveAttend.vue')
            },
            {
                path:"vocationlist",
                component:()=> import('../view/vocationlist.vue')
            },
            {
                path:"approveVocation",
                component:()=> import('../view/approveVocation.vue')
            },
            {
                path:"approverlist",
                component:()=> import('../view/approverlist.vue')
            },
            {
                path:"empworktype",
                component:()=> import('../view/empworktype.vue')
            },
            {
                path:"worktype",
                component:()=> import('../view/worktype.vue')
            },
            {
                path:"vocationtype",
                component:()=> import('../view/vocationtype.vue')
            },
            {
                path:"attendList",
                component:()=> import('../view/AttendList.vue')
            },
            {
                path:"test",
                component:()=> import('../view/test.vue')
            }
        ]
    }
    
    
];

//创建路由实例并传递 `routes` 配置
const router = createRouter({
    routes:r,
    history:createWebHashHistory()
});

router.beforeEach((to, from, next)=>{
    if(to.path == "/" || to.path == "/start/login" || to.path == "/login_forget"){
        next();
    }else{
        const store = userStore();
        let account = store.getAccount;
        if(account){
            next();
        }else{
            next("/start/login");
        }
    }
});

export default router;